package com.yanhui.utils.file.word;

import com.yanhui.api.ai.NaturalLanguageApi;
import org.apache.commons.lang3.StringUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ComputeTfIdf {

	public static void compute(String filepath,String outputPath,String docfile,String wordfile) throws FileNotFoundException, IOException{

		ReadFiles rf=new ReadFiles();

		//1.将文件读到map到，对应的是（filename，content）
		Map<String,String> doc_content=rf.readFileAllContent(filepath);

		//4.计算每个文档的单词的tfidf值并保存到文本
		for(Map.Entry<String, String> entry:doc_content.entrySet()){
			String filePath = "C:/yhfile/output.txt";
			String word = "中华人民共和国";
			String words=entry.getValue();
			List<String> terms = NaturalLanguageApi.getIkanalyzer(words);
			System.out.println("[【" + word + "】词频 ] " + computeTF(word, terms));
//			System.out.println("[【" + word + "】逆文档频率 ] " + computeIDF(filePath, word));
//			System.out.println("[【" + word + "】词频-逆文档频率 ] "+computeTFIDF(filePath,terms,word));
		}
	}
	/** * 计算一篇文章分词后除去标点符号后词的总数 * * @param terms 分词后的集合 * @return 一篇文章分词后除去标点符号后词的总数 */
	private static Integer countWord(List<String> terms) {
		if (terms == null || terms.size() == 0) {
			return null;
		}
		for (int i = 0; i < terms.size(); i++) {
			if ("null".equals(terms.get(i)) || terms.get(i).startsWith("w")) {
				terms.remove(i);
			}
		}
		return terms.size();
	}



	/** * 计算词频 IF * * @param word 词 * @param terms 分词结果集合 * @return IF */
	public static  double computeTF(String word, List<String> terms) {
		if (StringUtils.isBlank(word)) {
			return 0.0;
		}
		int count = 0;
		for (String term : terms) {
			if (term.equals(word)) {
				count += 1;
			}
		}
		return (double) count / countWord(terms);
	}

	/** * 统计词语的逆文档频率 IDF * * @param path 存放 url 的文件路径 * @param word IDF */
	/*public static  double computeIDF(String path, String word) {
		if (StringUtils.isBlank(path) || StringUtils.isBlank(word)) {
			return 0.0;
		}

		List<String> urls = readUrlFromText(path);
		int count = 1;
		for (String url : urls) {
			String text = getTextFromUrl(url);
			if (text.contains(word)) {
				count += 1;
			}
		}
		return Math.log10((double) urls.size() / count);
	}*/

	/**
	 * 计算词频-逆文档频率 TF—IDF
	 *   * @param filePath 存放url的文件路径
	 *   * @param terms 分词结果集合
	 *   * @param word 词
	 *   * @return TF—IDF
	 */

//	public static Double computeTFIDF(String filePath, List<String> terms, String word) {
//		return computeTF(word, terms) * computeIDF(filePath, word);
//	}

}
